using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
    using System.Drawing.Drawing2D;
    using System.Diagnostics;

namespace PerformanceCounters
{
    public partial class Form1 : Form
    {
        // Used to move the graph line
        int graphIncrement = 0;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // Get a list of available performance counter categories
            PerformanceCounterCategory[] perfCounters = PerformanceCounterCategory.GetCategories();
            for (int i = 0; i < perfCounters.Length; i++)
            {
                // Add the category to the drop-down list
                lstCats.Items.Add(perfCounters[i].CategoryName);
            }
        }

        private void tmrCounter_Tick(object sender, EventArgs e)
        {
            float currVal;
            // Move to and get the latest value in the performance counter
            currVal = perfMain.NextValue();
            // Update the label with the value
            lblValue.Text = Math.Round(Convert.ToDouble(currVal.ToString()), 2) + "%";
            Graphics gfx = picGraph.CreateGraphics();
            Pen pn = new Pen(Color.Red, 1);
            gfx.DrawLine(pn, graphIncrement, 200, graphIncrement, 199 - (currVal * 2));

            graphIncrement += 2;
            if (graphIncrement > 500)
            {
                picGraph.Invalidate();
                graphIncrement = 0;
            }
        }

        private void lstCats_SelectedIndexChanged(object sender, EventArgs e)
        {
            string[] instanceNames;
            System.Collections.ArrayList counters = new System.Collections.ArrayList();
            if (lstCats.SelectedIndex != -1)
            {
                System.Diagnostics.PerformanceCounterCategory mycat = new System.Diagnostics.PerformanceCounterCategory(this.lstCats.SelectedItem.ToString());
                // Remove the current contents of the list.
                this.lstCounters.Items.Clear();
                // Retrieve the counters.
                instanceNames = mycat.GetInstanceNames();
                if (instanceNames.Length == 0)
                {
                    counters.AddRange(mycat.GetCounters());
                }
                else
                {
                    for (int i = 0; i < instanceNames.Length; i++)
                    {
                        counters.AddRange(mycat.GetCounters(instanceNames[i]));
                    }
                }

                // Add the retrieved counters to the list.
                foreach (System.Diagnostics.PerformanceCounter counter in counters)
                {
                    this.lstCounters.Items.Add(counter.CounterName);
                }
            }
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            if (lstCats.SelectedIndex != -1 && lstCounters.SelectedIndex != -1 && lstInstances.SelectedIndex != -1)
            {
                // Clear the graph
                picGraph.Invalidate();
                graphIncrement = 0;
                perfMain.CategoryName = lstCats.SelectedItem.ToString();
                perfMain.CounterName = lstCounters.SelectedItem.ToString();
                perfMain.InstanceName = lstInstances.SelectedItem.ToString();
                tmrCounter.Start();
            }
            else
            {
                MessageBox.Show("Please select a category, counter and instance first.", "Selection needed", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void lstCounters_SelectedIndexChanged(object sender, EventArgs e)
        {
            // Clear the existing instance list
            lstInstances.Items.Clear();
            PerformanceCounterCategory perfCat = new PerformanceCounterCategory(lstCats.SelectedItem.ToString());
            string[] catInstances;
            catInstances = perfCat.GetInstanceNames();
            lstInstances.Items.Clear();
            foreach (string catInstance in catInstances)
            {
                lstInstances.Items.Add(catInstance);
            }
        }
    }
}